<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <th:block th:include="include :: header('地址检索')"/>
    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>
    <script type="text/javascript"
            src="https://api.map.baidu.com/api?v=2.0&ak=ZSXGYc4z7RLVClUmrlL1w9cH9DRghNuw"></script>
    <title>地址解析</title>
</head>
<body>
<input id="adress" style="width: 100%;border: none;line-height: 40px;padding: 0 10px;outline: none;"
       placeholder="请输入具体地址，按回车键获取经纬度" type="text">
<input id="lng" class="form-control " type="hidden">
<input id="lat" class="form-control " type="hidden">
<div id="allmap"></div>
<th:block th:include="include :: footer"/>
</body>
</html>
<script type="text/javascript">
    // <!--回显地图点位-->
    var lat = parent.$("#shopLatitude").val()
    var lng = parent.$("#shopLongitude").val()
    var shopAdress = parent.$("#shopAdress").val()
    $('#lng').val(lng)
    $('#lat').val(lat)
    $("#adress").val(shopAdress)

    var map = new BMap.Map("allmap");
    var point = new BMap.Point(lng, lat);
    var marker = new BMap.Marker(point);  // 创建标注
    map.addOverlay(marker);              // 将标注添加到地图中
    map.centerAndZoom(point, 16);
    map.enableScrollWheelZoom(true);
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放


    var gc = new BMap.Geocoder();
    //点击地图定位
    map.addEventListener("click", function (e) {
        //获取当前点击 位置的经纬度，并显示在文本框中
        $("#lat").val(e.point.lat);
        $("#lng").val(e.point.lng);
        map.clearOverlays();
        var new_point = new BMap.Point(e.point.lng, e.point.lat);
        var new_mk = new BMap.Marker(new_point);
        map.addOverlay(new_mk);
        map.panTo(new_point);
        gc.getLocation(new_point, function (rs) {
            //获取当前点击 位置的 省市县，街道号 和街道名称。并显示在 文本框中
            //街道号 和街道名称 不一定能获取到，有时候为空。
            var addComp = rs.addressComponents;
            var province = addComp.province;
            var city = addComp.city;
            var district = addComp.district;
            var street = addComp.street;
            var streetNumber = addComp.streetNumber;
            $("#adress").val(province + city + district + street + streetNumber)

        });

    });


    $('#adress').bind('keyup', function (event) {
        if (event.keyCode == "13") {
            //回车执行查询
            var adress = $("#adress").val();
            gc.getPoint(adress, function (point) {
                if (point) {
                    var lat = point.lat;
                    var lng = point.lng;
                    $("#lat").val(lat);
                    $("#lng").val(lng);
                    map.clearOverlays();
                    map.centerAndZoom(point, 16);
                    map.addOverlay(new BMap.Marker(point));
                } else {
                    alert("您选择地址没有解析到结果!");
                }
            }, "湖北省");
        }
    });


    function submitHandler() {
        var lng = $('#lng').val()
        var lat = $('#lat').val()
        var adress = $("#adress").val()
        if (!lng || !lat || !adress) {
            $.modal.alertWarning("位置未标记");
            return false
        }
        parent.$("#shopAdress").val(adress)
        parent.$("#shopLatitude").val(lat)
        parent.$("#shopLongitude").val(lng)
        $.modal.close();
    }

</script>